// 实体类
const Response = require('../entity/response');

// 工具函数
const { createUserJwtToken } = require('../util/server_jwt');

// 数据访问层
const loginDao = require('../dao/login');

const loginService = {
    async login(login) {
        try {
            let result = loginDao.login(login);
            if (result.length) {
                // 生成jwt
                let jwt = createUserJwtToken(result[0], {expiresIn: '168h'});
                result[0].token = jwt;
                return (new Response()).ok("登录成功", result[0]);
            } else {
                return (new Response()).fail("用户名或密码错误");
            }
        } catch (err) {
            return (new Response()).fail("登录失败");
        }
    },
};

module.exports = loginService;